package com.zhengyisky.controller;

import com.alibaba.fastjson.JSONObject;
import com.thoughtworks.xstream.XStream;
import com.zhengyisky.entity.WxTextMessage;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class WxController {

    @GetMapping("/wx")
    @ResponseBody
    public String wxGet(String signature, String timestamp, String nonce,String echostr){
        System.out.println("收到绑定请求....;");
        System.out.println(signature);
        System.out.println(timestamp);
        System.out.println(nonce);
        System.out.println(echostr);







        return echostr;
    }

    @PostMapping("/wx")
    @ResponseBody
    public String wxPost(HttpServletRequest request) throws Exception {
        System.out.println("收到微信消息。。。。");
        ServletInputStream inputStream = request.getInputStream();
        SAXReader saxReader = new SAXReader();
        Document doc = saxReader.read(inputStream);
        Element root = doc.getRootElement();
        List<Element> elements = root.elements();

        Map<String,String> requestMap = new HashMap<>();
        for (Element child:elements) {
            requestMap.put(child.getName(),child.getStringValue());
        }







        //封装需要回复的XML报文
        WxTextMessage wxTextMessage = new WxTextMessage();
        wxTextMessage.setFromUserName(requestMap.get("ToUserName"));
        wxTextMessage.setToUserName(requestMap.get("FromUserName"));
        wxTextMessage.setContent("您的消息我们已收到:"+requestMap.get("Content"));
        wxTextMessage.setMsgType("text");
        wxTextMessage.setCreateTime(System.currentTimeMillis());

        XStream xStream = new XStream();
        xStream.alias("xml",WxTextMessage.class);
        String responseXML = xStream.toXML(wxTextMessage);
        return responseXML;
    }

    @GetMapping("/bindAccount")
    public String bindAccount(String code) throws Exception{
        System.out.println(code);
        String appid = "wxba2fbc0446dbf2bb";
        String secret = "8a72c5abcb177f53ecc4935304bc5fd4";

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        CloseableHttpResponse response = httpClient.execute(post);
        String json = EntityUtils.toString(response.getEntity());

        Map map = JSONObject.parseObject(json, Map.class);

        return "redirect:/bindAccount.html?openid="+map.get("openid");
    }

}
